package com.qianchang.microfinance.view.base;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.res.ResourcesCompat;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.dm.library.http.base.HttpBase;
import com.dm.library.utils.StringUtils;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.event.LoginEvent;
import com.qianchang.microfinance.common.ActivityConstants;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.AppManager;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.MainActivity;
import com.qianchang.microfinance.view.login.LoginActivity;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import static com.qianchang.microfinance.utils.ErrorManager.ERROR_CODE_ACCOUNT_LOGIN_INVALID;

/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * 修 改 人:  jiaohongyun
 * 修改时间:  2016/8/24
 */
public class BaseActivity extends DmBaseActivity implements HttpBase.LoginCheckListener, HttpBase.AuthorizationCheckListener {

    private View titleBar;
    public static final String HAS_BASIC_INFO_KEY = "HAS_BASIC_INFO_KEY";

    public boolean isEventLoginSus = false;


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);

        MobclickAgent.onResume(this);   // 友盟基础指标统计，不能遗漏

    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);

        MobclickAgent.onPause(this); // 友盟基基础指标统计，不能遗漏
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 设置显示和影藏状态栏
     */
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        titleBar = findViewById(R.id.title_bar);
        initTitleBar();
    }

    /**
     * 初始化标题
     */
    private void initTitleBar() {
        if (titleBar != null) {
            titleBar.findViewById(R.id.tv_back).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    onBackPressed();
                    finish();
                }
            });
            ((DTextView) titleBar.findViewById(R.id.tv_title)).setTextContent(getString(R.string.app_name));
        }
    }

    /**
     * 设置标题
     */
    protected void setTitle(String title) {
        if (titleBar != null) {
            ((DTextView) titleBar.findViewById(R.id.tv_title)).setTextContent(title);
        }
    }

    /**
     * 设置页面Title
     */
    public void setTitle(int titleId) {
        if (titleBar != null) {
            ((DTextView) titleBar.findViewById(R.id.tv_title)).setTextContent(titleId);
        }
    }

    /**
     * 设置页面左边图片
     */
    public void setLeftContent(String leftContent, int imgId) {
        if (titleBar != null) {
            DTextView tvBack = (DTextView) titleBar.findViewById(R.id.tv_back);
            tvBack.setTextContent(leftContent);
            Drawable drawableLeft = ResourcesCompat.getDrawable(getResources(), imgId, null);
            if (null != drawableLeft) {
                drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(), drawableLeft.getMinimumHeight());//必须设置图片大小，否则不显示
                tvBack.setCompoundDrawables(drawableLeft, null, null, null);
            }
            tvBack.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏页面左边图片
     */
    public void hideLeftContent() {
        if (titleBar != null) {
            TextView tvBack = (TextView) titleBar.findViewById(R.id.tv_back);
            tvBack.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 设置页面右边图片
     */
    public void setRightContent(String leftContent, int imgId) {
        if (titleBar != null) {
            DTextView tvRight = (DTextView) titleBar.findViewById(R.id.tv_right);
            tvRight.setTextContent(leftContent);
            Drawable drawableRight = ResourcesCompat.getDrawable(getResources(), imgId, null);
            if (null != drawableRight) {
                drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(), drawableRight.getMinimumHeight());//必须设置图片大小，否则不显示
                tvRight.setCompoundDrawables(drawableRight, null, null, null);
            }
            tvRight.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 设置页面右边文字 2018.1.23鸿胜科技新增
     */
    public void setRightContent(String leftContent) {
        if (titleBar != null) {
            DTextView tvRight = (DTextView) titleBar.findViewById(R.id.tv_right);
            tvRight.setTextContent(leftContent);
            tvRight.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置页面右边文字Clidck 2018.1.23鸿胜科技新增
     */
    public void setRightConentOnClick(View.OnClickListener onClickListener){
        if (titleBar != null) {
            DTextView tvRight = (DTextView) titleBar.findViewById(R.id.tv_right);
            tvRight.setOnClickListener(onClickListener);
        }
    }

    /**
     * 隐藏页面右边图片
     */
    public void hideRightContent() {
        if (titleBar != null) {
            titleBar.findViewById(R.id.tv_right).setVisibility(View.INVISIBLE);
        }
    }


    protected void initView() {
    }

    protected void initData() {

    }

    /***
     * 设置title
     * @param title
     */
    protected void setTitleName(String title) {
        DTextView mTitle = (DTextView) titleBar.findViewById(R.id.tv_title);
        if (null != mTitle) {
            mTitle.setTextContent(title);
        }
    }


    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        startActivity(intent);
    }


    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, boolean finish) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        startActivity(intent);
        if (finish) {
            finish();
        }
    }


    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }


    /**
     * 含有Bundle通过Class跳转界面,并判断是否销毁当前界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle, boolean finish) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        if (finish) {
            finish();
        }
    }


    /**
     * 含有Bundle通过Class跳转界面,并判断是否销毁当前界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }


    protected Bundle getBundle() {
        Intent intent = getIntent();
        Bundle bundle = null;
        if (null != intent) {
            bundle = intent.getExtras();
        }
        return bundle;
    }

    public static final String ACTION_RESULT_NORMAL_LOGIN = "login_result_normal";

    @Override
    public void goLogin() {
        if (!(this instanceof MainActivity)) {
            finish();
        }
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setAction(ACTION_RESULT_NORMAL_LOGIN);
        startActivityForResult(intent, ActivityConstants.REQUEST_CODE_NORMAL_LOGIN);
        BaseActivity.this.finish();
    }

    @Override
    public boolean checkLogin() {
        return DMApplication.getInstance().isLogin();
    }

    @Override
    public List<String> getWhiteList() {
        List<String> whiteList = new ArrayList<>();
        whiteList.add(DMConstant.ApiURL.FIND_APP_INDEX_SET);
        whiteList.add(DMConstant.ApiURL.FIND_SITE_LIST);
        whiteList.add(DMConstant.ApiURL.FIND_USER_BASIC_INFO);
        whiteList.add(DMConstant.ApiURL.GET_PRODUCT_LIST);
        whiteList.add(DMConstant.ApiURL.REGISTER);

        whiteList.add(DMConstant.ApiURL.LOGIN);
        whiteList.add(DMConstant.ApiURL.SEND_IDENTIFY_CODE);
        whiteList.add(DMConstant.ApiURL.FIND_TGREEMENT_NAME);
        whiteList.add(DMConstant.ApiURL.FIND_ABOUT);
        whiteList.add(DMConstant.Config.FILE_IP); // 新添加的apk更新url  2018.3.9 px

        whiteList.add(DMConstant.ApiURL.CHECK_USER_PHONE_IS_EXIST);
        whiteList.add(DMConstant.ApiURL.UPDATE_FORGET_TRAN_PWD);
        whiteList.add(DMConstant.ApiURL.UPDATE_PWD_BY_PHONE);
        whiteList.add(DMConstant.ApiURL.FIND_AGREEMENT_COMMON);
        return whiteList;
    }

    @Override
    public List<String> getInvalidCodes() {
        List<String> invalidCodes = new ArrayList<>();
        invalidCodes.add(ERROR_CODE_ACCOUNT_LOGIN_INVALID);
        return invalidCodes;
    }

    @Override
    public void doInvalidCodeAction(String code, String json) {
        if (StringUtils.isEmptyOrNull(code)) {
            return;
        }
        switch (code) {
            case ERROR_CODE_ACCOUNT_LOGIN_INVALID:
                AppManager.getAppManager().finishOtherActivities(MainActivity.class);
                DMApplication.getInstance().setUserLoginInfo(null);
                startActivity(LoginActivity.class);
                EventBus.getDefault().post(new LoginEvent(LoginEvent.LoginStatue.LOGOUT));
                break;
        }
    }

    protected boolean checkAllCommitAble(ArrayList<Boolean> commitList) {
        for (Boolean aBoolean : commitList) {
            if (!aBoolean) {
                return false;
            }
        }
        return true;
    }

    protected ArrayList<Boolean> initCommitList(int size) {
        ArrayList<Boolean> booleanList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            booleanList.add(false);
        }
        return booleanList;
    }

    public void colseSoftKey(EditText mEditText) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }

    public void showDataError() {
        ToastUtil.getInstant().show(this, getString(R.string.data_error));
    }
}
